package com.chen.controller;


import com.chen.entity.AddressBook;
import com.chen.entity.R;
import com.chen.service.AddressBookService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.List;

@RestController
@RequestMapping("/addressBook")
public class AddressBookController {
    @Autowired
    private AddressBookService addressBookService;

    @GetMapping("/list")
    public R list(HttpSession session){
        Long userId = (Long) session.getAttribute("user");
      List< AddressBook> list=addressBookService.findlist(userId);
        return R.success(list);
    }
    @PostMapping
    public R add(@RequestBody AddressBook addressBook,HttpSession session){
     Long userId= (Long) session.getAttribute("user");
        addressBook.setUserId(userId);
        addressBookService.add(addressBook);
        return R.success(null);
    }

    @PutMapping("/default")
    public R addressBookdefault(@RequestBody AddressBook addressBook){
        addressBookService.addressBookdefault(addressBook);
        return R.success(null);
    }

    @GetMapping("{id}")
    public R getbyid(@PathVariable Long id){
       AddressBook addressBook= addressBookService.get(id);
       return R.success(addressBook);
    }

    @PutMapping
    public R update(@RequestBody AddressBook addressBook){
        addressBookService.updateAddressBook(addressBook);
        return R.success(null);
    }

    @DeleteMapping
    public R deletebyids(Long[] ids){
        addressBookService.deleteByIds(ids);
        return R.success(null);
    }

    @GetMapping("/default")
    public R getdefault(){
        AddressBook  addressBook  =addressBookService.getdefault();
        return R.success(addressBook);
    }
}
